package com.dgut.admodule.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.admodule.entity.Ad;
import com.dgut.admodule.entity.dto.AdQueryDTO;
import com.dgut.admodule.service.IAdService;
import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtPageable;



@Controller
@RequestMapping("/ad")
public class AdController {
	@Autowired
	private IAdService adService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Ad ad) {
		try {
			adService.saveOrUpdate(ad);
			return new ExtAjaxResponse(true,"保存成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"保存失败！");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{
		try {
			Ad ad =  adService.findOne(id);
			if(null!=ad) {
				adService.delete(ad);
			}
			return new ExtAjaxResponse(true,"删除成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！");
		}
	}
	
	@RequestMapping("/deleteAds")
	public @ResponseBody ExtAjaxResponse deleteStaffs(@RequestParam Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids);
			if(null!=idLists) {
				adService.delete(idLists);
			}
			return new ExtAjaxResponse(true,"删除成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Ad findOne(@RequestParam Long id) 
	{
		Ad ad =  adService.findOne(id);
		
		return ad;
	}
	
	@RequestMapping("/findNew")
	public @ResponseBody Ad findNew() 
	{
		return adService.findNew();
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Ad> findAll() 
	{
		 List<Ad> lists =  adService.findAll();
		
		return lists;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<Ad> findPage(AdQueryDTO adQueryDTO,ExtPageable pageable) 
	{
		pageable.setSort("adId");
		Page<Ad> page =  adService.findAll(AdQueryDTO.getSpecification(adQueryDTO), pageable.getPageable());
		
		return page;
	}
}
